package edu.pitt.cs.zhangfan.sa.core;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import edu.pitt.cs.zhangfan.sa.io.ExcelReader;
import edu.pitt.cs.zhangfan.sa.model.Document;
import edu.pitt.cs.zhangfan.sa.model.DocumentPair;
import edu.pitt.cs.zhangfan.sa.model.EditDiff;
import edu.pitt.cs.zhangfan.sa.model.Sentence;

public class AnnotatorAssit {
	public ArrayList<DocumentPair> loadPairs(String path) throws IOException {
		ArrayList<DocumentPair> docPairs = new ArrayList<DocumentPair>();
		ExcelReader reader = new ExcelReader();

		File folder = new File(path);
		File[] subs = folder.listFiles();
		for (int i = 0; i < subs.length; i++) {
			DocumentPair dp = reader.readDocs(subs[i].getAbsolutePath());
			docPairs.add(dp);
		}
		return docPairs;
	}

	public String getDiff(String src, String dst) {
		if (src.equals(dst)) {
			return null;
		} else {
			int diffIndex = 0;
			for (int i = 0; i < src.length(); i++) {
				if (src.charAt(i) != dst.charAt(i)) {
					diffIndex = i;
					break;
				}
			}
			if (diffIndex >= dst.length()) {
				return "";
			}
			return dst.substring(diffIndex, dst.length()) + "\n" +"[Original sentence:"+src+"]";
		}
	}

	public ArrayList<EditDiff> processPairs(ArrayList<DocumentPair> pairs) {
		ArrayList<EditDiff> editdiffs = new ArrayList<EditDiff>();
		for (int i = 0; i < pairs.size(); i++) {
			EditDiff editDiff = new EditDiff(pairs.get(i).getFileName());
			DocumentPair docPair = pairs.get(i);
			Document src = docPair.getSrc();
			Document dst = docPair.getModified();

			int[] marker = new int[src.getSentences().size()];
			for (int j = 0; j < marker.length; j++) {
				marker[j] = 0;
			}

			ArrayList<Sentence> sentences = dst.getSentences();
			String lastOrigNo = "-1";
			for (int j = 0; j < sentences.size(); j++) {
				int dstIndex = j + 1;
				ArrayList<Integer> origIndex = sentences.get(j)
						.getAlignedIndex();
				if (origIndex.size() > 1) {
					// Multiple alignment to one sentence
					String origNo = "";
					for (int k = 0; k < origIndex.size(); k++) {
						int scannedIndex = origIndex.get(k);
						marker[scannedIndex - 1] = 1;
						origNo += origIndex.get(k) + ",";
					}
					origNo = origNo.substring(0, origNo.length() - 1);
					lastOrigNo = origNo;
					editDiff.addUnit(origNo, Integer.toString(dstIndex),
							sentences.get(j).getContent(), "Merge");
				} else if (origIndex.size() == 0) {
					editDiff.addUnit(lastOrigNo, Integer.toString(dstIndex),
							sentences.get(j).getContent(), "ADD");
				} else {
					marker[origIndex.get(0)-1] = 1;
					String origNo = Integer.toString(origIndex.get(0));
					String origSent = src.getSentences()
							.get(origIndex.get(0) - 1).getContent();
					String sentNo = Integer.toString(dstIndex);
					String dstSent = sentences.get(j).getContent();
					String diff = getDiff(origSent, dstSent);
					if (diff != null) {
						editDiff.addUnit(origNo, sentNo, dstSent, diff);
						lastOrigNo = origNo;
					}
				}
			}

			// Adding the removed ones
			for (int j = 0; j < marker.length; j++) {
				if (marker[j] == 0) {
					String origNo = Integer.toString(j + 1);
					editDiff.addUnit(origNo, "NA", src.getSentences().get(j)
							.getContent(), "REMOVED");
				}
			}
			editDiff.arrange();
			editdiffs.add(editDiff);
		}
		return editdiffs;
	}

	public void printDiffs(String rootPath, ArrayList<EditDiff> diffs)
			throws IOException {
		for (int i = 0; i < diffs.size(); i++) {
			EditDiff diff = diffs.get(i);
			String filePath = rootPath + "/" + diff.getFileName().replace("\\.", "_")+".txt";
			BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
			writer.write(diff.toString());
			writer.close();
		}
	}

	public static void main(String[] args) throws IOException {
		AnnotatorAssit aa = new AnnotatorAssit();
		ArrayList<DocumentPair> pairs = aa
				.loadPairs("E:\\independent study\\Revision\\dev_data\\annotations");
		ArrayList<EditDiff> diffs = aa.processPairs(pairs);
		aa.printDiffs("E:\\independent study\\Revision\\dev_data\\diffs", diffs);
	}
}
